Skip to content

fix: add missing odd numbers to CPU core/processor count mapping - #718

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/eaglefrom
GongHeng2017:agent/bugfix/8a187793
Jul 31, 2026
Merged

fix: add missing odd numbers to CPU core/processor count mapping#718
deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/eaglefrom
GongHeng2017:agent/bugfix/8a187793

Conversation

@GongHeng2017

@GongHeng2017 GongHeng2017 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

根因分析

设备管理器 CPU 概况的数字映射表 DeviceCpu::m_trNumber 只收录了 1 和偶数,不含任何大于 1 的奇数。当逻辑处理器数为奇数(如本机 9)时,getOverviewInfo()m_trNumber.value(9, "") 查表落空返回空串,导致数字被静默丢弃,显示成 (六核/逻辑处理器) 而缺少「九」。

关键证据:

  • DeviceCpu.cpp:10-71m_trNumber 表 62 条中唯一奇数是 1,其余全为偶数
  • DeviceCpu.cpp:193-202getOverviewInfo()m_trNumber.value(N, "") 查表,落空返回空串无回退
  • 服务端 cpuinfo.cpp:91-101 logicalNum() 累加逻辑核数,结果可为奇数(非对称线程配置)

修复方案

m_trNumber 映射表中补齐 3 至 127 的全部奇数条目,使任意奇数核数/逻辑处理器数都能查到对应文本,不再落空丢失数字。

改动安全评估

低风险。改动为纯数据表扩展——仅在静态 const 映射表中新增 63 条条目,不涉及任何函数签名、逻辑流程或调用方变更,对现有偶数场景行为无影响。

Summary by Sourcery

Bug Fixes:

  • Ensure CPU overview displays correct text for systems with an odd number of logical processors by adding missing odd-number mappings to the core count table.

1. 在 DeviceCpu::m_trNumber 映射表中补齐 3 至 127 的全部奇数条目;
2. 解决逻辑处理器数为奇数时查表落空、数字丢失不显示的问题;

=====================================

1. added all missing odd number entries (3-127) to DeviceCpu::m_trNumber map;
2. fixed logical processor count being dropped when it is an odd number;

Log: 修复设备管理器处理器概况中逻辑处理器数为奇数时数量显示缺失的问题

Bug: https://pms.uniontech.com/bug-view-372319.html

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @GongHeng2017, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@sourcery-ai

sourcery-ai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Reviewer's Guide

Extends the CPU core count translation map so that all odd numbers from 3 to 127 have localized string entries, preventing missing numeric words when displaying CPU overview for systems with an odd number of logical processors.

Sequence diagram for CPU overview number lookup with extended odd mappings

sequenceDiagram
  actor User
  participant DeviceManagerUI
  participant CpuInfo
  participant DeviceCpu
  participant m_trNumber

  User->>DeviceManagerUI: openCpuOverview
  DeviceManagerUI->>CpuInfo: logicalNum
  CpuInfo-->>DeviceManagerUI: coreCount
  DeviceManagerUI->>DeviceCpu: getOverviewInfo coreCount
  DeviceCpu->>m_trNumber: value coreCount ""
  m_trNumber-->>DeviceCpu: localizedNumberText
  DeviceCpu-->>DeviceManagerUI: overviewText_with_number
  DeviceManagerUI-->>User: displayCpuOverview
Loading

File-Level Changes

Change Details Files
Complete the CPU-core-count-to-text translation table to include all odd values so odd logical processor counts render correctly in the overview.
  • Add QT_TR_NOOP entries for every odd integer between 3 and 127 that were previously missing from the m_trNumber QMap
  • Keep existing even-number entries and special higher values (128, 192, 256) unchanged
  • Ensure textual representations for new entries follow the existing English phrasing pattern (e.g., Twenty-one, Ninety-nine, One hundred and twenty-three).
deepin-devicemanager/src/DeviceManager/DeviceCpu.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:85分

■ 【总体评价】

代码补全了CPU核心数的翻译映射表,修复了奇数核心无法正确显示英文单词的问题
逻辑正确且无安全漏洞,但存在大量硬编码,代码可维护性较差扣15分

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

代码在 DeviceCpu::m_trNumber 映射表中插入了缺失的奇数核心数键值对,语法完全符合 C++ 和 Qt 框架规范,数据结构定义正确。
潜在问题:无
建议:无

  • 2.代码质量(一般)✕

DeviceCpu.cpp 中,通过硬编码方式逐一列举 1 到 128 的数字与英文单词的映射。这种实现方式导致代码冗长且高度重复,后续若需扩展核心数支持范围,必须手动添加大量条目,极易遗漏且维护成本高。
潜在问题:硬编码扩展性差;存在人为拼写遗漏风险;违背 DRY(Don't Repeat Yourself)原则
建议:将数字到英文单词的转换逻辑提取为独立函数,利用算法动态生成英文单词,或采用更紧凑的数组结构结合循环生成;若必须保留映射表,建议通过脚本自动生成此部分代码以保证完整性。

  • 3.代码性能(无性能问题)✓

QMap 在初始化时构建静态映射表,查询时间复杂度为 O(log n),对于设备管理器这种低频调用的场景,性能完全满足需求,不存在性能瓶颈。
潜在问题:无
建议:无

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
本次修改仅为静态常量数据的补充,不涉及任何外部输入处理、内存操作或权限交互,不存在安全风险。

  • 建议:无

■ 【改进建议代码示例】

// 建议将静态映射表替换为动态生成函数,提高可维护性
// 以下为示例逻辑,实际实现可根据需要调整

const char* DeviceCpu::getNumberString(int number) {
    static const char* ones[] = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
                                 "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    static const char* tens[] = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

    static QHash<int, QString> cache;
    if (cache.contains(number)) {
        return cache.value(number).toUtf8().constData();
    }

    QString result;
    if (number == 0) {
        result = "Zero";
    } else if (number < 20) {
        result = ones[number];
    } else if (number < 100) {
        result = tens[number / 10];
        if (number % 10 != 0) {
            result += "-" + QString(ones[number % 10]).toLower();
        }
    } else {
        result = "One hundred";
        if (number % 100 != 0) {
            int rem = number % 100;
            result += " and ";
            if (rem < 20) {
                result += ones[rem].toLower();
            } else {
                result += tens[rem / 10].toLower();
                if (rem % 10 != 0) {
                    result += "-" + QString(ones[rem % 10]).toLower();
                }
            }
        }
    }
    
    cache[number] = result;
    return cache.value(number).toUtf8().constData();
}

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: GongHeng2017, max-lvs

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@GongHeng2017

Copy link
Copy Markdown
Contributor Author

/merge

@GongHeng2017

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot
deepin-bot Bot merged commit 107a0e8 into linuxdeepin:develop/eagle Jul 31, 2026
20 checks passed
@deepin-bot

deepin-bot Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unknown)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants